package com.hl.toolkit.server.bdpay;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class RefundServlet   {

	/**
	 * 构建初始化信息变量
	 */
	private ServletConfig scon = null;
	private  static String service_code="service_code=2";
	private  static String input_charset="input_charset=1";
	private  static String sign_method="sign_method=1";
	private  static String output_type="output_type=1";
	private  static String output_charset="output_charset=1";
	private  static String currency ="currency=1";
	private  static String BFB_REFUND_URL=BaiduConfig.BaiduAddressRefund;
  
 	
	public RefundServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public String doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 		request.setCharacterEncoding("gbk");
       String result="";
		/**
		 * get values from refund.html begin
		 */
		String return_url = "return_url="+BaiduConfig.applicationNotiyReturnUrl;
		String return_url1 = "return_url="+URLEncoder.encode(BaiduConfig.applicationNotiyReturnUrl,"gbk");
	    //String return_method ="return_mothod="+2;非必填参数 
		String version = "version=2";
	    String sp_no="sp_no="+BaiduConfig.spNo;
	    
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");  
  	    String order_no="order_no="+request.getParameter("paymentId");
		String cashback_amount = "cashback_amount="+request.getParameter("Amount");
		Calendar   c   =   Calendar.getInstance(); 
 		String mDateTime=formatter.format(c.getTime());  
		String cashback_time = "cashback_time="+mDateTime.substring(0, 14);
		String sp_refund_no = "sp_refund_no=" +mDateTime.substring(0, 14)+(int)(Math.random() * 100);
		
		/**
		 * get values from pay_unlogin.html end
		 */
		
		
	    //生成签名
	    String [] array = {service_code,input_charset,sign_method,output_type,output_charset,return_url,
	    		version,sp_no,order_no,cashback_amount,cashback_time,currency,sp_refund_no};
	    String sign= "sign="+new BfbSdkComm().make_sign(array); 
	    String [] array1 = {cashback_amount,cashback_time,currency,input_charset,order_no,output_charset,output_type,return_url1,service_code,
	    		 sign_method,sp_no,sp_refund_no,version,sign};    
		//提交退款请求，跳转到百度钱包收银台
		HttpRequester requester = new HttpRequester();
		HttpRespons hr = requester.sendArrayParam(BFB_REFUND_URL, "GET", array1, null);
		System.out.println(hr+"...11111111111111111");
	// Structs2Util.render(response, hr.getContent());
		result=hr.getContent();
		System.out.println("Content:"+hr.getContent());
		System.out.println("tostring:"+hr.toString());
 		return result;
	}
	//从配置获取参数
	public void init(ServletConfig config) throws ServletException {
		scon = config;
 		  //版本
	    String version="version=2";
	    //加密方式md5或者hash
	    sign_method="sign_method=1";
		sign_method ="sign_method=1";
		output_type ="output_type=1";
		output_charset ="output_charset=1";
		BFB_REFUND_URL =BaiduConfig.BaiduAddressRefund;
		currency ="currency=1";
	}
	
	public String getServlectParam(String param){
		return scon.getServletContext().getInitParameter(param);
	}
 


}
